package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.Permission;
import com.zyg.service.PermissionService;
import com.zyg.util.LayData;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/permission")
public class PermissionController {


    @Resource
    private PermissionService permissionService;

    @RequestMapping("/tolist")
    public  String tolist(){
        return "permission/list";
    }


    @RequestMapping("/list")
    @ResponseBody
    public  String list(Permission permission, @RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "limit", defaultValue = "10") Integer limit){
        // 查询数据
        LayData layData = new LayData();
        QueryWrapper<Permission> qw = new QueryWrapper<>();
        qw.like(permission.getMenuName() != null && !"".equals(permission.getMenuName()),"menu_name",permission.getMenuName());
        long totalCount = permissionService.count(qw);
        layData.setCount((int) totalCount);
        Page<Permission> page1 = new Page<>(page, limit);
        layData.setData(permissionService.list(page1, qw));
        layData.setCode(0);
        layData.setMsg("查询成功");
        // 数据返回
        String data = JSON.toJSONString(layData);
        return data;
    }



}
